package org.platform.processor.monitor.job;

import lombok.extern.slf4j.Slf4j;
import org.platform.mo.bussmo.diagnose.event.DeviceBO;
import org.platform.mo.bussmo.diagnose.event.DevicePropertyBO;
import org.platform.mo.bussmo.diagnose.event.StandardEventBO;
import org.platform.processor.monitor.data.CacheLibraryPO;
import org.platform.processor.monitor.data.InitDataSource;
import org.springframework.boot.ApplicationArguments;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.Queue;

/**
 * 第二步:  该类主要从{@link CacheLibraryPO }中取设备信息,并不断解析
 *
 * @author FengJie
 * @date 2018/5/16
 */

@Service
@Slf4j
public class ResolveDevicePropertyJob {
    private static final int RESOLVE_THREAD_COUNT = 0;
    private static final int RESOLVE_SLEEP_TIME = 1000;

    @PostConstruct
    private void init() {
        //队列存放所有通过zeroMQ发送过来的设备信息
        for (int i = 0; i < RESOLVE_THREAD_COUNT; i++) {
            ResolveThread thread = new ResolveThread();
            thread.setName("Resolve " + i);
            thread.start();
        }
    }

    class ResolveThread extends Thread {
        Map<String, List<StandardEventBO>> standardReferenceSourceMap = CacheLibraryPO.getCacheLibraryPO().getStandardReferenceSourceMap();
        Queue<DeviceBO> deviceQueue = CacheLibraryPO.getCacheLibraryPO().getDeviceQueue();
        Queue<DevicePropertyBO> devicePropertyQueue = CacheLibraryPO.getCacheLibraryPO().getDevicePropertyQueue();

        @Override
        @SuppressWarnings("InfiniteLoopStatement")
        public void run() {
            while (true) {
                try {
                    DeviceBO device = deviceQueue.poll();
                    if (null != device) {
                        resolve(device);
                    } else {
                        Thread.sleep(RESOLVE_SLEEP_TIME);
                    }
                } catch (Exception e) {
                    log.error("解析设备线程异常" + e.getMessage() + "; 继续解析设备信息");
                }
            }
        }

        /**
         * 先判断当前属性是否存在于标准参考数据源里面,如果在:
         * 将设备信息解析成对应的参数,添加到设备属性队列里
         * {@link InitDataSource#run(ApplicationArguments)} 在这里设计标准数据源的key
         */
        void resolve(DeviceBO bo) {
            if (null != bo.getPropertyMap()) {
                bo.getPropertyMap().forEach((k, v) -> {
                    //  设备id_属性id
                    String key = bo.getDeviceId() + "_" + k;
                    if (standardReferenceSourceMap.containsKey(key)) {
                        DevicePropertyBO property = new DevicePropertyBO();
                        property.setDeviceId(bo.getDeviceId());
                        property.setTimeSpan(bo.getTimeSpan());
                        property.setPropertyName(k);
                        property.setPropertyValue(v);
                        devicePropertyQueue.offer(property);
                    }
                });
            }
        }
    }
}
